<template>
  <div class="app-container">
    <div class="mb-20">
      <el-button class="btn-active" icon="el-icon-plus" size="mini" @click="goAdd">新增设备</el-button>
      <el-button class="btn-delete" icon="el-icon-delete" size="mini" @click="deleteRow">删除</el-button>
    </div>
    <div class="search">
      <el-form ref="form" :model="form" label-width="80px" inline>
        <el-form-item label="">
          <el-select v-model="form.deviceStatus" placeholder="启用状态">
            <el-option label="未启用" value="1" />
            <el-option label="已启用" value="0" />
          </el-select>
        </el-form-item>
        <el-form-item label="">
          <el-date-picker
            v-model="form.createTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="创建时间"
          />
        </el-form-item>
        <el-form-item label="">
          <el-input v-model="form.createUserName" placeholder="创建人员" />
        </el-form-item>
        <el-form-item label="">
          <el-input v-model="form.deviceName" placeholder="设备名称" />
        </el-form-item>
        <el-form-item label="">
          <el-button size="mini" icon="el-icon-search" @click="search">查询</el-button>
          <el-button size="mini" icon="el-icon-refresh-right" @click="search(1)">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-background="rgba(0, 0, 0, 0.8)"
      element-loading-text="加载中"
      border
      fit
      highlight-current-row
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
      />
      <el-table-column align="center" label="序号" width="95">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="设备名称">
        <template slot-scope="scope">
          {{ scope.row.deviceName }}
        </template>
      </el-table-column>
      <el-table-column label="设备信息" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.remark }}</span>
        </template>
      </el-table-column>
      <el-table-column label="IP" width="130" align="center">
        <template slot-scope="scope">
          {{ scope.row.deviceIp }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="端口" width="130" align="center">
          <template slot-scope="scope">
            {{ scope.row.pageviews }}
          </template>
        </el-table-column> -->
      <el-table-column label="厂家" width="130" align="center">
        <template slot-scope="scope">
          {{ scope.row.deviceFactory }}
        </template>
      </el-table-column>
      <el-table-column class-name="测试状态" label="启用状态" width="110" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.deviceStatus == 0" class="green-tag">启用</span>
          <span v-else class="red-tag">未启用</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" width="110" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.createUserName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" width="110" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="操作" width="200">
        <template slot-scope="scope">
          <el-button class="detail" icon="el-icon-edit-outline" @click="goEdit(scope.row)">编辑</el-button>
          <el-button v-if="scope.row.controlPath" class="detail" icon="el-icon-menu" @click="window.open(scope.row.controlPath)">控制</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="currentPage"
      :page-sizes="[10, 20, 30, 50]"
      :page-size="100"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    </el-pagination>
    <!-- <router-view /> -->
  </div>
</template>

<script>
import { getDeviceList, deleteDevice } from '@/api/table'
import { removeDevice } from '@/api/form'
export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      form: {},
      list: null,
      listLoading: true,
      currentPage: 1,
      pageNum: 10,
      total: 0,
      multipleSelection: []
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    deleteRow() {
      console.log(this.multipleSelection)
      removeDevice({ ids: this.multipleSelection.map(item => item.deviceId).join(',') }).then(res => {
        this.$message.success('操作成功')
        this.fetchData()
      })
    },
    search(num) {
      if (num == 1) {
        this.form = {}
      }
      this.currentPage = 1
      this.fetchData()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleSizeChange(val) {
      this.pageNum = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.fetchData()
    },
    goAdd() {
      this.$router.push('/assets/device/add')
    },
    goEdit(row) {
      this.$router.push({ path: '/assets/device/add', query: { id: row.deviceId }})
    },
    deleteDevice(row) {
      deleteDevice({ stringids: row.deviceId }).then(res => {
        console.log(res)
      })
    },
    fetchData() {
      this.listLoading = true
      const data = { deviceName: '测试设备' }
      getDeviceList(
        { pageSize: this.pageNum,
          pageNum: this.currentPage,
          ...this.form
        }
      ).then(res => {
        this.list = res.rows
        this.total = res.total
        this.listLoading = false
      })
    }
  }
}
</script>
